//
//  HealthSubmitController.m
//  village_ios
//
//  Created by mac on 2019/8/9.
//  Copyright © 2019 com.sw.village. All rights reserved.
//

#import "HealthSubmitController.h"
#import "HealthPaySubmitRequest.h"
#import "HealthPaySubmitResponse.h"
#import "UserInfo.h"
#import "SettingManager.h"
#import "HealthSubmitUtils.h"


@interface HealthSubmitController ()
@property (strong, nonatomic) IBOutlet UILabel *tipLabel;

//个人
@property (strong, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UIImageView *avatorImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *villageJobLabel;
@property (weak, nonatomic) IBOutlet UIImageView *partyIconView;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;

//房租配置
@property (weak, nonatomic) IBOutlet UILabel *shopNumberLabel;
@property (weak, nonatomic) IBOutlet UILabel *rentalNumberLabel;
@property (weak, nonatomic) IBOutlet UILabel *otherNumberLabel;

@property (weak, nonatomic) IBOutlet UILabel *shopMonthFeeLabel;
@property (weak, nonatomic) IBOutlet UILabel *rentalMonthFeeLabel;
@property (weak, nonatomic) IBOutlet UILabel *otherMonthFeeLabel;

@property (weak, nonatomic) IBOutlet UILabel *shopFeeLabel;
@property (weak, nonatomic) IBOutlet UILabel *rentalFeeLabel;
@property (weak, nonatomic) IBOutlet UILabel *otherFeeLabel;

@property (weak, nonatomic) IBOutlet UILabel *feeLabel;

@property (weak, nonatomic) IBOutlet UIButton *actionButton;

//data
@property (strong, nonatomic) HealthPaySubmitResponse *healthPaySubmitResponse;
@property (strong, nonatomic) UserInfo *userInfo;

//content
@property (strong, nonatomic) NSString *appealContent; //申诉理由
@property (strong, nonatomic) NSString *submitAgainContent; //申请再提交理由


@end

@implementation HealthSubmitController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadNetworkUserInfo];
    [self loadNetworkData];
}
//
- (void)loadNetworkData {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if(self.healthPaySubmitId) {
        [params addEntriesFromDictionary:@{@"healthPaySubmitId":self.healthPaySubmitId}];
    }
    [SYProgressHUD show];
    [APIManager requestWithUrlString:@"api/health/submit/detail" params:params method:@"GET" success:^(id response) {
        self.healthPaySubmitResponse = [HealthPaySubmitResponse mj_setKeyValues:response];
        [self loadReponse];
        [self reloadAllData];
    } failure:^(NSError *error) {
        [self reloadErrorData];
    }];
}
//加载用户信息
- (void)loadNetworkUserInfo {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [SYProgressHUD show];
    [APIManager requestWithUrlString:@"api/user/info" params:params method:@"GET" success:^(id response) {

        self.userInfo = [UserInfo mj_setKeyValues:response];
        self.nameLabel.text = self.userInfo.name;
        self.phoneLabel.text = self.userInfo.phone;
        self.villageJobLabel.text = self.userInfo.name;
        [self.avatorImageView setImageWithURL:[NSURL URLWithString:self.userInfo.avator] placeholderImage:[UIImage imageNamed:@"app_icon"]];
        self.partyIconView.hidden = self.userInfo.verifyId != nil;
        [SYProgressHUD hide];
    } failure:^(NSError *error) {
        [SYProgressHUD hide];
    }];
}

//
- (void)loadReponse {

    //
    NSInteger status = self.healthPaySubmitResponse.status;

    self.addressLabel.text = self.healthPaySubmitResponse.address;
    self.title =  [HealthSubmitUtils getSubmitStatusDesc:status];
    self.tipLabel.text = [HealthSubmitUtils getSubmitTipDesc:status];

    //button
    NSDictionary *buttonDict = [HealthSubmitUtils getActionButtonDict:status];
    [self.actionButton setTitle:buttonDict[@"title"] forState:(UIControlState) 0];
    SEL sel = NSSelectorFromString(buttonDict[@"selector"]);
    [self.actionButton addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];

    //price
    self.shopNumberLabel.text =  [NSString stringWithFormat:@"%@",@(self.healthPaySubmitResponse.shopNumber)] ;
    self.rentalNumberLabel.text =  [NSString stringWithFormat:@"%@",@(self.healthPaySubmitResponse.rentalNumber)] ;
    self.otherNumberLabel.text =  [NSString stringWithFormat:@"%@",@(self.healthPaySubmitResponse.otherNumber)] ;
    [self reloadTotalPrice];
}
//刷新价格
- (void)reloadTotalPrice {

    CGFloat shopMonthFee = [[[SettingManager sharedInstance] dict][@"shopMonthFee"] floatValue];
    CGFloat rentalMonthFee = [[[SettingManager sharedInstance] dict][@"rentalMonthFee"] floatValue];
    CGFloat otherMonthFee = [[[SettingManager sharedInstance] dict][@"otherMonthFee"] floatValue];

    self.shopMonthFeeLabel.text = [NSString stringWithFormat:@"%@元/月",@(shopMonthFee)];
    self.rentalMonthFeeLabel.text = [NSString stringWithFormat:@"%@元/月",@(rentalMonthFee)];
    self.otherMonthFeeLabel.text = [NSString stringWithFormat:@"%@元/月",@(otherMonthFee)];

    CGFloat shopFee = shopMonthFee * [self.shopNumberLabel.text integerValue];
    CGFloat rentalFee = rentalMonthFee * [self.rentalNumberLabel.text integerValue];
    CGFloat otherFee = otherMonthFee * [self.otherNumberLabel.text integerValue];

    self.shopFeeLabel.text = [NSString stringWithFormat:@"%@元/月",@(shopFee)];
    self.rentalFeeLabel.text = [NSString stringWithFormat:@"%@元/月",@(rentalFee)];
    self.otherFeeLabel.text = [NSString stringWithFormat:@"%@元/月",@(otherFee)];
}

#pragma mark - Action
- (void)toSubmit {
}
- (void)toAppeal {
}
- (void)toPay {
}

- (void)toRequest {
    
    //生成申请 申请改变
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];

    // 如果加载详情注入请求体
    if (self.healthPaySubmitResponse) {
        NSDictionary *dict = [self.healthPaySubmitResponse mj_keyValues];
        [self.healthPayRequest mj_setKeyValues:dict];
    }

    //切换地址
    if (self.addressLabel.text) {
        self.healthPayRequest.address = self.addressLabel.text;
    }
    //申报失败 - 申诉内容
    if (self.appealContent) {
        self.healthPayRequest.appealContent = self.appealContent;
    }

    [params addEntriesFromDictionary:[self.healthPayRequest mj_keyValues]];
    [SYProgressHUD show];
    [APIManager requestWithUrlString:@"api/healthSubmit/addItem" params:params method:@"POST" success:^(id response) {
        [self loadNetworkData];
    } failure:^(NSError *error) {
        [SYProgressHUD showFailureText:@"加载失败"];
    }];
}

@end
